package com.qire.manhua.view;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.qire.common.utils.EquipmentInfoUtil;
import com.qire.manhua.R;

import androidx.annotation.Nullable;

public class ContinuousSignInDisplayBoard extends View {

    private Drawable drawable_w;
    private Drawable drawable_y;
    private int drawableWidth = 0;
    private int drawableHeight = 0;
    private int daysNumber = 0;

    private Node[] nodes = {
            new Node("10",1),new Node("15",2),new Node("20",3),
            new Node("25",4),new Node("30",5),new Node("35",6),new Node("40",7)};


    public ContinuousSignInDisplayBoard(Context context) {
        super(context);
        init();
    }

    public ContinuousSignInDisplayBoard(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ContinuousSignInDisplayBoard(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        drawable_w = getResources().getDrawable(R.drawable.app_task_center_icon_wqd, null);
        drawable_y = getResources().getDrawable(R.drawable.app_task_center_icon_yqd, null);

        drawableWidth  = drawable_w.getIntrinsicWidth();
        drawableHeight = drawable_w.getIntrinsicHeight();
        drawable_w.setBounds(0,0,drawableWidth,drawableHeight);
        drawable_y.setBounds(0,0,drawableWidth,drawableHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int offset = getWidth()/nodes.length;
        int moveX = offset/2 - (drawableWidth/2);
        for(Node node : nodes){
            canvas.save();
            canvas.translate(moveX,0);
            node.onDraw(canvas);
            canvas.restore();
            moveX += offset;
        }
    }



    public void setDaysNumber(final int daysNumber){
        if(this.daysNumber != daysNumber) {
            postDelayed(() -> new ProgressAnimation(daysNumber).start(), 300);
        }
    }

    public void setNodes(String[] nodes){
        if(nodes==null)
            return;
        if(nodes.length != this.nodes.length)
            return;
        for(int index=0;index < this.nodes.length;index++){
            this.nodes[index].setText(nodes[index]);
        }
    }

    private class Node {

        private final int redColor  = Color.parseColor("#ff5e03");
        private final int grayColor = Color.parseColor("#eeeeee");

        private final int grayTextColor = Color.parseColor("#a6a6a6");
        private final int whiteTextColor = Color.WHITE;

        private String text = "0";
        private final  int index;

        private Paint paint     = new Paint(Paint.ANTI_ALIAS_FLAG);
        private Rect textRect  = new Rect();

        public Node(String text, int index){
            setText(text);
            this.index = index;
        }

        public void setText(String text){
            this.text = text;
        }

        protected void onDraw(Canvas canvas) {

            int cx = drawableWidth/2;
            int cy = drawableHeight/2;

            paint.setTextSize(EquipmentInfoUtil.dip2px(11));
            paint.getTextBounds(text,0,text.length(),textRect);
            paint.setColor(index > daysNumber ? grayTextColor : whiteTextColor);
            int textX = cx - textRect.width()/2;
            int textY = cy + textRect.height() + EquipmentInfoUtil.dip2px(3);

            (index > daysNumber ? drawable_w : drawable_y).draw(canvas);
            canvas.drawText(text,textX,textY,paint);


            int radius = EquipmentInfoUtil.dip2px(4);
            int dotX = cx;
            int dotY = drawableHeight + radius;
            //padding
            dotY = dotY + EquipmentInfoUtil.dip2px(4);

            paint.setStrokeWidth(EquipmentInfoUtil.dip2px(2));
            if(index!=1) {
                paint.setColor(index > daysNumber ? grayColor : redColor);
                canvas.drawLine(0, dotY, dotX, dotY, paint);
            }

            if(index!=7) {
                int offset = getWidth() / nodes.length;
                paint.setColor(index < daysNumber ? redColor : grayColor);
                canvas.drawLine(dotX, dotY, offset, dotY, paint);
            }

            paint.setColor(index > daysNumber ? grayColor : redColor);
            paint.setStrokeWidth(EquipmentInfoUtil.dip2px(4));
            canvas.drawCircle(dotX, dotY, radius, paint);

            String daysText = index+"天";
            paint.setTextSize(EquipmentInfoUtil.dip2px(12));
            paint.setColor(Color.parseColor("#5c5c5c"));
            Rect daysTextRect = new Rect();
            paint.getTextBounds(daysText,0,daysText.length(),daysTextRect);
            int daysTextX = dotX - daysTextRect.width()/2;
            int daysTextY = dotY + daysTextRect.height() + EquipmentInfoUtil.dip2px(14);
            canvas.drawText(daysText,daysTextX,daysTextY,paint);
        }

    }

    private class ProgressAnimation extends ValueAnimator implements AnimatorUpdateListener {
        private final int number;
        public ProgressAnimation(int number){
            this.number = number;
            addUpdateListener(this);
            setFloatValues(0,1);
            setDuration(number * 100);
        }

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float fraction = getAnimatedFraction();
            daysNumber = (int)(number * fraction);
            invalidate();
        }
    }


}
